//
//  SearchGodViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/20.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "SearchGodViewController.h"

#import "SearchResultTableViewController.h"
#import "PowerViewController.h"
#import "EvenRedViewController.h"
#import "RewardViewController.h"
#import "UserDetailViewController.h"
#import "HotGodCell.h"
//#import <PYSearch.h>

#import "GreatGodViewModel.h"
#import "GreatGodModel.h"
#import "UserDetailViewController.h"

@interface SearchGodViewController ()
<
    UITableViewDelegate,
    UITableViewDataSource,
    UISearchResultsUpdating,
    UISearchBarDelegate,
    UISearchControllerDelegate
>
@property (nonatomic,strong) UITableView * tableView;

@property (strong, nonatomic) UISearchController *searchController;

@property (nonatomic,strong) SearchResultTableViewController *result;

@property (nonatomic,strong) UIView * tempView;

@property (nonatomic,strong) GreatGodViewModel * greatViewModel;

@end

@implementation SearchGodViewController

-(GreatGodViewModel *) greatViewModel{
    if (!_greatViewModel) {
        _greatViewModel  = [[GreatGodViewModel alloc] init];
    }
    return _greatViewModel;
}

-(UITableView *) tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_CONTENT_INTABBAR_HEIGHT) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        
        [MXUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
    }
    return _tableView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem setTitle:@"搜索大神"];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.tableView];
    
    [self createTempView];
    
    
    self.result = [SearchResultTableViewController new];
    
    __weak SearchGodViewController *weakSelf = self;
    self.result.pushViewControllerBlock = ^(SearchGodModel *model) {
        
        UserDetailViewController * userdetail = [[UserDetailViewController alloc] init];
        [userdetail setUserID:model.searchUserId];
        [weakSelf.navigationController pushViewController:userdetail animated:YES];
        
    };
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.result];
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.placeholder = @"搜索大神";
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = YES;
    self.searchController.searchBar.returnKeyType  = UIReturnKeySearch;
    self.searchController.searchBar.delegate = self;
    //搜索结果的代理设置
    self.searchController.searchResultsUpdater = self;
    
    [self.searchController.searchBar sizeToFit];
    self.searchController.searchBar.barTintColor = DEF_COLOR_F0F0F0;
    self.definesPresentationContext = YES;
    [self.tableView setTableHeaderView:self.searchController.searchBar];
    
    UISearchBar *searchBar = self.searchController.searchBar;
    UIImageView *barImageView = [[[searchBar.subviews firstObject] subviews] firstObject];
    barImageView.layer.borderColor = DEF_COLOR_F0F0F0.CGColor;
    barImageView.layer.borderWidth = 1;
    
    /**
     热爆大神网络请求
     */
    [self greatNetWork];
}

-(void) greatNetWork{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    [[self.greatViewModel findUserBuyInfoRequestParameter:@"4"] subscribeNext:^(id x) {
        
        [self.tableView reloadData];
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return [self.greatViewModel.finduserArrayList count];

}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * str = @"cell";
    HotGodCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HotGodCell" owner:self options:nil] lastObject];
        
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(15, cell.height - 1, DEF_DEVICE_WIDTH - 15, 1)];
        [view setBackgroundColor:DEF_COLOR_F0F0F0];
        [cell addSubview:view];
    }
    GreatGodModel * model = [self.greatViewModel.finduserArrayList objectAtIndex:indexPath.row];
    [cell setGreatCellDataSrouce:model];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UserDetailViewController * userdetail = [[UserDetailViewController alloc] init];
    
    GreatGodModel * model = [self.greatViewModel.finduserArrayList objectAtIndex:indexPath.row];
    [userdetail setUserID:model.greatuserId];
    
    [self.navigationController pushViewController:userdetail animated:YES];

}


-(void) pwerClick:(UITapGestureRecognizer *) gesture{
    
    PowerViewController * power = [[PowerViewController alloc] init];
    [self.navigationController pushViewController:power animated:YES];
}

-(void) redClick:(UITapGestureRecognizer *) gesture{
    
    EvenRedViewController * even = [[EvenRedViewController alloc] init];
    [self.navigationController pushViewController:even animated:YES];
}

-(void) rewardClick:(UITapGestureRecognizer *) gesture{
    
    RewardViewController * rewar = [[RewardViewController alloc] init];
    [self.navigationController pushViewController:rewar animated:YES];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    
    [self.tempView setHidden:NO];
    
    
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    
    [self.tempView setHidden:YES];
}


#pragma mark UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    [self.tableView reloadData];
    
    if (![MXUtility NSNULLString:searchController.searchBar.text]) {
        [self searchNetWork:searchController.searchBar.text];
    }
    
    
    searchController.searchBar.showsCancelButton = YES;
    
    UIView * view = [searchController.searchBar.subviews objectAtIndex:0];
    
    for (UIView *subView in view.subviews) {
        
        if ([subView isKindOfClass:[UIButton class]]) {
            
            UIButton *bar = (UIButton *)subView;
            
            [bar setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            
            [bar setTitle:@"取消" forState:UIControlStateNormal];
            
        }
    }
}

-(void) searchNetWork:(NSString *) searchName{
    
    [[self.greatViewModel searchUserNameRequestParameter:searchName] subscribeNext:^(id x) {
        
        if (x) {
            
            [self.result setArrayList:self.greatViewModel.searchArrayList];
            
        }
    }error:^(NSError *error) {
        
    }];
}
-(void)createTempView{
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.tableView.width, self.tableView.height)];
    [self.tempView setBackgroundColor:[UIColor whiteColor]];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((CGRectGetWidth(self.tempView.frame) - 50)/2, (CGRectGetHeight(self.tempView.frame) - 154)/2, 50, 50)];
    imageview.image = [UIImage imageNamed:@"wushuju"];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    [self.tempView addSubview:imageview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,imageview.height+10 + imageview.y, CGRectGetWidth(self.tempView.frame), 30)];
    label.textColor = DEF_COLOR_RGB(215, 215, 215);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];
    label.text = @"暂无大神数据";
    [self.tempView addSubview:label];
    
    [self.tableView addSubview:self.tempView];
    self.tempView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
